package com.jx.service.impl;

import com.jx.dao.UserMapper;
import com.jx.entity.User;
import com.jx.service.UserService;
import com.jx.utils.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    public User selectByPrimaryKey(int i) {
        return userMapper.selectByPrimaryKey(i);
    }

    @Override
    public User getUserByName(String username) {
        return userMapper.getUserByName(username);
    }

    @Override
    public PageResult<User> getUserList(Integer page, Integer limit, String searchKey, String searchValue) {
     List<User> users = userMapper.getUserList(page,limit,searchKey,searchValue);
        List<User> uList = userMapper.getUserTotle();
        return new PageResult(uList.size(),users);
    }

    @Override
    public List<User> getUserByUser(User u) {
        return userMapper.getUserByUser(u);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updateByPrimaryKeySelective(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int register(User user) {
        return userMapper.insert(user);
    }

    @Override
    @Transactional(rollbackOn=Exception.class)
    public int updateByPrimaryKeySelective2(){
        User user = getUserByName("Tom");
        user.setAge((user.getAge()) -1);
        int i = userMapper.updateByPrimaryKeySelective(user);
        int a = 10/0;
        User user2 = getUserByName("Rose");
        user2.setAge((user.getAge()) +1);
        int i2 = userMapper.updateByPrimaryKeySelective(user);


        return i;
    }


}
